const names = [111, 222, 333]
// 数组的有限迭代器
function arrIterator(arr) {
    let index = 0
    return {
        next: () => {
            if (index < names.length) {
                return { done: false, value: names[index++] }
            } else {
                return { done: true, value: undefined }
            }
        }
    }
}
const namesIterator = arrIterator(names)
console.log(namesIterator.next());
console.log(namesIterator.next());
console.log(namesIterator.next());
console.log(namesIterator.next());

// 数字无限迭代器
function numIteraotr() {
    let index = 0
    return {
        next: () => {
            return { done: false, value: index++ }
        }
    }
}

let numinter = numIteraotr()
let k = numinter.next()
while (!k.done) {
    console.log(k);
    k = numinter.next()
}